package com.ecommerce.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ecommerce.common.CommonEnum;
import com.ecommerce.common.ResultBody;
import com.ecommerce.entity.Advertising;
import com.ecommerce.service.AdvertisingService;
import com.ecommerce.utils.WrapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 广告Controller
 *
 * @author wang
 */
@RestController
@Api(value = "AdvertisingController", tags = {"广告接口"})
@RequestMapping("/advertising")
public class AdvertisingController {

    @Autowired
    private WrapperUtils wrapperUtils;

    @Autowired
    private AdvertisingService advertisingService;

    /**
     * 查询广告列表
     *
     * @param advertising 广告
     * @param pageNum     页码
     * @param pageSize    条数
     * @return 广告列表
     */
    @ApiOperation(value = "查询广告列表", notes = "查询广告列表")
    @PostMapping(value = "/advertisingList")
    public ResultBody advertisingList(@RequestBody(required = false) Advertising advertising, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        IPage<Advertising> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Advertising> wrapper = wrapperUtils.advertisingWrapper(advertising);
        wrapper.orderByDesc(Advertising::getCreateTime);
        return ResultBody.ok().data(advertisingService.page(page, wrapper));

    }


    /**
     * 创建广告
     *
     * @param advertising 广告
     * @return 创建广告
     */
    @ApiOperation(value = "创建广告", notes = "创建广告")
    @PostMapping(value = "/createAdvertising")
    public ResultBody createAdvertising(@RequestBody Advertising advertising) {
        advertising.setStatus(CommonEnum.SHELVES.getCode());
        advertising.setDeleted("0");
        advertising.setCreateTime(new Date());
        boolean save = advertisingService.save(advertising);
        if (save) {
            return ResultBody.ok().message("创建成功");
        } else {
            return ResultBody.error().message("创建失败");
        }
    }

    /**
     * 上架广告
     *
     * @param advertising 广告
     * @return 上架广告
     */
    @ApiOperation(value = "上架广告", notes = "上架广告")
    @PostMapping(value = "/shelvesAdvertising")
    public ResultBody shelvesAdvertising(@RequestBody Advertising advertising) {
        Long adId = advertising.getAdId();
        if (adId != null) {
            LambdaUpdateWrapper<Advertising> advertisingWrapper = new LambdaUpdateWrapper<>();
            advertisingWrapper.eq(Advertising::getAdId, adId).set(Advertising::getStatus, CommonEnum.SHELVES.getCode());
            boolean update = advertisingService.update(advertisingWrapper);
            if (update) {
                return ResultBody.ok().message("上架广告成功");
            } else {
                return ResultBody.error().message("上架广告失败");
            }
        }
        return ResultBody.error().message("上架广告失败");

    }


    /**
     * 下架广告
     *
     * @param advertising 广告
     * @return 下架广告
     */
    @ApiOperation(value = "下架广告", notes = "下架广告")
    @PostMapping(value = "/soldOutAdvertising")
    public ResultBody soldOutAdvertising(@RequestBody Advertising advertising) {
        Long adId = advertising.getAdId();
        if (adId != null) {
            LambdaUpdateWrapper<Advertising> advertisingWrapper = new LambdaUpdateWrapper<>();
            advertisingWrapper.eq(Advertising::getAdId, adId).set(Advertising::getStatus, CommonEnum.UN_SHELVES.getCode());
            boolean update = advertisingService.update(advertisingWrapper);
            if (update) {
                return ResultBody.ok().message("下架广告成功");
            } else {
                return ResultBody.error().message("下架广告失败");
            }
        }
        return ResultBody.error().message("下架广告失败");
    }

    /**
     * 删除广告
     *
     * @param advertising 广告
     * @return 删除广告
     */
    @ApiOperation(value = "删除广告", notes = "删除广告")
    @PostMapping(value = "/deleteAdvertising")
    public ResultBody deleteAdvertising(@RequestBody Advertising advertising) {
        Long adId = advertising.getAdId();
        if (adId != null) {
            boolean delete = advertisingService.removeById(adId);
            if (delete) {
                return ResultBody.ok().message("删除广告成功");
            } else {
                return ResultBody.error().message("删除广告失败");
            }
        } else {
            return ResultBody.error().message("删除广告失败");
        }
    }
}
